package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysRoleResource;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysRoleResourceService;
import com.zuipin.util.Pagination;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Controller
 * 
 * @ClassName: SysRoleResourceController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 09:21:09
 */
@Api(value = "", description = "")
@RestController
@RequestMapping(value = "/sysRoleResource")
public class SysRoleResourceController {
	@Resource
	public ISysRoleResourceService sysRoleResourceService;
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询", notes = "查询")
	public Result<SysRoleResource> getSysRoleResourceById(@ApiParam(required = true, name = "id", value = "主键") @RequestParam Long id) {
		SysRoleResource sysRoleResource = sysRoleResourceService.findById(id);
		Result<SysRoleResource> result = new Result<SysRoleResource>();
		if (sysRoleResource != null) {
			result.setData(sysRoleResource);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	@RequestMapping(value = "/del", method = RequestMethod.DELETE)
	@ApiOperation(value = "根据主键删除", notes = "删除")
	public Result<SysRoleResource> delSysRoleResource(Long id) {
		Result<SysRoleResource> result = new Result<SysRoleResource>();
		sysRoleResourceService.delete(id);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/seach", method = RequestMethod.POST)
	@ApiOperation(value = "搜索SKU并分页", notes = "搜索并分页")
	@Valids({ @Valid(name = "sysRoleResource.roleId", maxLength = 19), //
			@Valid(name = "sysRoleResource.resourceId", maxLength = 19), //
			@Valid(name = "page.pageNumber", maxValue = 999), //
			@Valid(name = "page.pageSize", maxValue = 999), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRoleResource.roleId", value = "角色id", required = true, dataType = "roleId", paramType = "query"),
			@ApiImplicitParam(name = "sysRoleResource.resourceId", value = "资源(按纽/功能)id", required = true, dataType = "resourceId", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysRoleResource>> seachSysRoleResource(@Inject @ApiIgnore SysRoleResource sysRoleResource, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysRoleResource>> result = new PageResult<List<SysRoleResource>>();
		result.setData(sysRoleResourceService.seachSysRoleResource(sysRoleResource, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/insertOrUpdateSysRoleResource", method = RequestMethod.POST)
	@ApiOperation(value = "六期-新增/修改-zhouhuihang", notes = "六期-新增/修改")
	@Valids({ @Valid(name = "sysRoleResource.id", maxLength = 19), //
			@Valid(name = "sysRoleResource.roleId", maxLength = 19), //
			@Valid(name = "sysRoleResource.resourceId", maxLength = 19), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRoleResource.id", value = "主键", required = false, dataType = "id", paramType = "query"),
			@ApiImplicitParam(name = "sysRoleResource.roleId", value = "角色id", required = true, dataType = "roleId", paramType = "query"),
			@ApiImplicitParam(name = "sysRoleResource.resourceId", value = "资源(按纽/功能)id", required = true, dataType = "resourceId", paramType = "query"), })
	public Result<SysRoleResource> insertOrUpdateSysRoleResource(@Inject @ApiIgnore SysRoleResource sysRoleResource) {
		Result<SysRoleResource> result = new Result<SysRoleResource>();
		if (sysRoleResource.getId() == null) {
			sysRoleResourceService.insert(sysRoleResource);
			result.setData(sysRoleResource);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("新增成功");
			return result;
		} else {
			sysRoleResourceService.update(sysRoleResource);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("修改成功");
			return result;
		}
	}
	
}
